/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *                               神兽保佑            永无BUG
 * 
 * @Author: start
 * @Date: 2023-04-21 09:06:11
 * @FilePath: \testKoa\src\routes\users.ts
 * @LastEditors: xing
 * @LastEditTime: 2023-04-22 15:46:42
 */

import Router from 'koa-router';
const router = new Router();
import query from '../utils/mysql';
router.prefix('/users');
import crypto from "../utils/crypto"
const svgCaptcha = require("svg-captcha")

//获取验证码
let captchaCode = ''

router.get("/getcaptchr", async ctx => {
    console.log(111111111111111);

    var captcha = svgCaptcha.create({
        size: 4,
        noise: 2,
        color: true,
        background: "#fff",
        height: 40,
        width: 100,
        fontSize: 50,
    })
    captchaCode = captcha.text;
    // captcha = ctx.response.type = "svg"
    console.log(captcha.data);

    ctx.body = captcha.data;
})





const foo = async (ctx: Router.RouterContext, next: any) => {
    let { account, password } = ctx.request.body as any
    let newPassword = crypto(password)
    let sql = `select * from users where account='${account}' and password='${newPassword}'`
    let data = await query(sql);
    console.log(data.length);

    if (data.length) {
        ctx.body = {
            code: 200, msg: "登录成功"
        }
    } else {
        ctx.body = {
            code: 400,
            msg: "账号或密码不正确"
        }
    }

};
router.post('/sign', foo);

const bar = async (ctx: Router.RouterContext, next: any) => {
    let { account, password } = ctx.request.body as any
    let sql = `select * from users where account='${account}'`
    let res = await query(sql)
    console.log(res.length);

    if (res.length) {
        ctx.body = {
            code: 400,
            msg: "该账号已注册"
        }
        return;
    } else {
        let mdpassword = crypto(password)
        console.log(mdpassword);
        console.log(111);
        let sql = `insert into users(account,password) value('${account}','${mdpassword}')`
        let res = await query(sql)
        // console.log(data);
        if (res.affectedRows > 0) {
            ctx.body = {
                code: 200,
                msg: "注册成功"
            }
            return;
        } else {
            ctx.body = {
                code: 400,
                msg: "注册失败"
            }
            return;
        }


    }

};

router.post('/registerUser', bar);

export default router;
